//
//  BCIndustryScreeningRiskLevelView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCIndustryScreeningRiskLevelView.h"

//layout
#import "UICollectionViewLeftAlignedLayout.h"
//cell
#import "BCIndustryScreeningDisplayCell.h"
//model
#import "BCIndustryRiskLevelModel.h"

@interface BCIndustryScreeningRiskLevelView () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout>

@property (nonatomic, strong) UICollectionView *riskCollection;

@end

@implementation BCIndustryScreeningRiskLevelView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.customTitleLabel.text = nil;
    //
    UICollectionViewLeftAlignedLayout *flowLayout = [UICollectionViewLeftAlignedLayout new];
    self.riskCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(BCIndustryScreeningBaseLeftPadding, 0, ScreenWidth-2*BCIndustryScreeningBaseLeftPadding, 0) collectionViewLayout:flowLayout];
    self.riskCollection.dataSource = self;
    self.riskCollection.delegate = self;
    self.riskCollection.backgroundColor = [UIColor clearColor];
    self.riskCollection.scrollEnabled = false;
    [self addSubview:self.riskCollection];
    [self.riskCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customTitleLabel.mas_bottom);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self).offset(-2*BCIndustryScreeningBaseLeftPadding);
        make.bottom.mas_equalTo(self);
    }];
    //
    [self.riskCollection registerClass:BCIndustryScreeningDisplayCell.class forCellWithReuseIdentifier:NSStringFromClass(BCIndustryScreeningDisplayCell.class)];
}

- (void)setSource:(NSArray<BCAllIndustryDataFlowRiskItem *> *)source {
    _source = source;
    //
    [self.riskCollection reloadData];
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.source.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCAllIndustryDataFlowRiskItem *model = self.source[indexPath.row];
    BCIndustryScreeningDisplayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCIndustryScreeningDisplayCell.class) forIndexPath:indexPath];
    cell.displayContent = model.text;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = floorf((collectionView.frame.size.width-2*10.0)/3);
    CGFloat height = 32.0f;
    return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //如果已经选择取消选中
    BCAllIndustryDataFlowRiskItem *model = self.source[indexPath.row];
    if (self.riskLevel==model) {
        [collectionView deselectItemAtIndexPath:indexPath animated:true];
        self.riskLevel = nil;
    }else {
        self.riskLevel = model;
    }
}



- (CGFloat)viewHeight {
    CGFloat height = [super viewHeight];
    height += self.riskCollection.collectionViewLayout.collectionViewContentSize.height;
    return height;
}

@end
